import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import '../src/assets/index.less'
import store from './store/index'
import axios from 'axios'
import http from './api/http'

const app = createApp(App).use(router).use(ElementPlus).use(store)
app.config.globalProperties.$axios = http.$axios
app.mount('#app')

/**
 * 路由守卫
 */
// 前置钩子
router.beforeEach((to, from, next) => {
  // console.log('**路由beforeEach**')
  // console.log(to, from, next)
  // console.log('**路由beforeEach**')
  next()
})
// 后置钩子
router.afterEach((to, from, failure) => {
  // if (!failure) sendToAnalytics(to.fullPath)
  // console.log('**路由afterEach**')
  // console.log(to, from, failure)
  // console.log('**路由afterEach**')
})
/**
 * 请求拦截
 */
// 请求时
axios.interceptors.request.use(
  config => {
    store.commit('showLoading')
    return config
  },
  error => {
    store.commit('hideLoading')
    return Promise.reject(error)
  }
)
// 请求返回时
axios.interceptors.response.use(
  response => {
    store.commit('hideLoading')
    return response
  },
  error => {
    if (error.response) {
      store.commit('hideLoading')
      return Promise.reject(error.response.data)
    }
  }
)
